use std::error::Error;
use bytes::Bytes;
use futures::{SinkExt, StreamExt};
use tokio::net::{TcpStream};
use tokio_util::codec::{Framed, LengthDelimitedCodec};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>>{


    let address = std::env::args().nth(1).unwrap_or("127.0.0.1:8888".to_string());

    let stream = TcpStream::connect(&address).await.expect("connect failed");
    let mut framed = Framed::new(stream, LengthDelimitedCodec::new());

    framed.send(Bytes::from("time")).await.unwrap();

    // stream.write_all(b"time").await.unwrap();


    let data = framed.next().await.unwrap();
    match data {
        Ok(data) => {
            println!("recv: {}", String::from_utf8(data.to_vec()).unwrap());
        },
        Err(e) => {
            println!("error: {}", e);
        }
    }
    Ok(())


}